﻿package com.tvein.sanguo.swc.action.login
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import fl.controls.TextInput;
	import flash.text.TextFormat;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class LoginCreate extends MovieClip
	{
		public static const NAME:String = "Login-Create";
		
		private var effectVol:int = 100;
		
		private var txtAccount:TextInput;
		private var txtPassword:TextInput;
		private var txtConfirm:TextInput;
		
		public function LoginCreate()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			var style:TextFormat = new TextFormat();
			style.size = 20;
			style.font = "LiSu";
			style.align = "center";
			
			txtAccount = new TextInput();
			txtAccount.name = "txtAccount";
			txtAccount.x = 100;
			txtAccount.y = 110;
			txtAccount.width = 400;
			txtAccount.height = 25;
			
			txtAccount.setStyle("textFormat", style);
			
			txtPassword = new TextInput();
			txtPassword.name = "txtPassword";
			txtPassword.x = 100;
			txtPassword.y = 210;
			txtPassword.width = 400;
			txtPassword.height = 25;
			txtPassword.displayAsPassword = true;
			
			txtPassword.setStyle("textFormat", style);
			
			txtConfirm = new TextInput();
			txtConfirm.name = "txtConfirm";
			txtConfirm.x = 100;
			txtConfirm.y = 310;
			txtConfirm.width = 400;
			txtConfirm.height = 25;
			txtConfirm.displayAsPassword = true;
			
			txtConfirm.setStyle("textFormat", style);
			
			txtAccount.addEventListener(FocusEvent.FOCUS_OUT, _onFocusOut);
			txtPassword.addEventListener(FocusEvent.FOCUS_OUT, _onFocusOut);
			txtConfirm.addEventListener(FocusEvent.FOCUS_OUT, _onFocusOut);
			
			this.addChild(txtAccount);
			this.addChild(txtPassword);
			this.addChild(txtConfirm);
			
			txtAccount.tabIndex = 1;
			txtPassword.tabIndex = 2;
			txtConfirm.tabIndex = 3;
			btnSend.tabIndex = 4;
			btnBack.tabIndex = 5;
			
			btnSend.visible = false;
			
			btnSend.addEventListener(MouseEvent.CLICK, onClick);
			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnSend.removeEventListener(MouseEvent.CLICK, onClick);
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function _onFocusOut(evt:FocusEvent):void
		{
			var style1:TextFormat = new TextFormat();
			style1.color = 0x999999;
			var style2:TextFormat = new TextFormat();
			style2.color = 0xFF0000;
			
			switch(evt.currentTarget.name)
			{
				case "txtAccount":
					var pattern:RegExp=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([\.]\w+)*/;
			
					if(pattern.test(txtAccount.text))
					{
						txtAccInfo.setTextFormat(style1);
					}
					else
					{
						txtAccInfo.setTextFormat(style2);
					}
					
					break;
				case "txtPassword":
					if(txtPassword.length > 5 && txtPassword.length < 129)
					{
						txtPswInfo.setTextFormat(style1);
					}
					else
					{
						txtPswInfo.setTextFormat(style2);
					}
					
					break;
				case "txtConfirm":
					if(txtPassword.text == txtConfirm.text)
					{
						txtConfirmInfo.setTextFormat(style1);
					}
					else
					{
						txtConfirmInfo.setTextFormat(style2);
					}
					
					break;
			}
		}

		private function _keyDownHandler(evt:KeyboardEvent):void
		{
			var pattern:RegExp=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([\.]\w+)*/;

			btnSend.visible = ((txtPassword.length > 5 && txtPassword.length < 129) && (txtPassword.text == txtConfirm.text) && (pattern.test(txtAccount.text)));
		}

		private function _resizeUI(evt:Event = null):void
		{
			this.x = (stage.stageWidth - this.width) * .5;
			
			if ((stage.stageHeight - this.height) > 100)
			{
				this.y = (stage.stageHeight - this.height) * .5;
			}
			else
			{
				this.y = 50;
			}
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnSend":
					btnSend.visible = false;
					btnBack.visible = false;
					dispatchEvent(new SwcEvent(NAME, ["-userCreate", [txtAccount.text, txtPassword.text]]));
					break;
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", "enter"]));
					break;
			}
		}
	}
}